package com.jfinal.club._admin.dajuan;

import com.jfinal.aop.Inject;
import com.jfinal.club._admin.exam.ExamAdminService;
import com.jfinal.club._admin.shiti.ShitiAdminService;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Dajuan;
import com.jfinal.club.common.model.Dajuanxiangqing;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

public class DajuanAdminController extends BaseController {

	@Inject
	DajuanAdminService dsrv;
	@Inject
	ShitiAdminService ssrv;
	@Inject
	ExamAdminService esrv;

	public void index() {
		Page<Dajuan> dajuanPage = dsrv.paginate(getParaToInt("p", 1));// 状态判断
		setAttr("dajuanPage", dajuanPage);
		render("index.html");
	}

	public void dati() {
		Page<Dajuan> dajuanPage = dsrv.paginate(getParaToInt("p", 1));
		setAttr("dajuanPage", dajuanPage);
		render("dati.html");
	}

	public void add_edit() {
		Page<Dajuan> dajuanPage = dsrv.paginate(getParaToInt("p", 1));
		setAttr("dajuanPage", dajuanPage);
		render("add_edit.html");
	}

	public void upload() {
		setAttr("tid", getPara("tid"));
		setAttr("examid", getPara("examid"));
		render("dati.html");
	}
	 public void distinguish () {
		 renderJson(dsrv.Handwriting());
	 }
	 public void compare() {
			dsrv.compare();
			renderJson(dsrv.plus());
		}
	 public void plus() {
			renderJson(dsrv.plus());
		// return dsrv.plus();
		}
	 public void score() {
		 Page<Dajuan> dajuanPage = dsrv.score(getParaToInt("p",1), getLoginAccountId());
			setAttr("dajuanPage", dajuanPage);
			render("score.html");

		}
	public void update() {
		Dajuanxiangqing dajuanxiangqing = getBean(Dajuanxiangqing.class);
		UploadFile uf = null;
		try {
			uf = getFile("vurl", dsrv.getDatiTempDir(), dsrv.getDatiMaxSize());
			if (uf == null) {
				renderJson(Ret.fail("msg", "请先选择上传文件"));
//				return;
			} else {
//				Dajuan dajuan = new Dajuan();
				int examid = getParaToInt("examid");
				System.out.print(examid);
				int tid = getParaToInt("tid");
				//// dajuan.setId(getParaToInt());
				 Ret ret = dsrv.update(examid,getLoginAccountId(),tid, uf,dajuanxiangqing);
				 renderJson(ret);
			}
		} catch (Exception e) {

			if (uf != null) {
				// 只有出现异常时才能删除，不能在 finally 中删，因为后面需要用到上传文件
				uf.getFile().delete();
			}
			renderJson(Ret.fail("msg", e.getMessage()));
		}
	}

}
